﻿using System;
using System.Drawing;
using FSky;
using System.Windows.Forms; // 确保引用了System.Windows.Forms命名空间
using static System.Net.Mime.MediaTypeNames;
namespace idea0
{
    public class Class1
    {
        // 定义一个15行25列的二维数组，表示游戏板
        public int[,] Board = new int[15, 25];
        // 每个单元格的大小，60像素
        private const int cellSize = 60;
        // 用于绘制游戏板的位图
        public Bitmap BoardBitmap;
        // 图片路径或名称
        public string image;
        // 坐标变量
        public int x = 0;
        public int y = 0;
        public System.Drawing.Image RoadImage { get; set; }
        public System.Drawing.Image WallImage { get; set; }
        public Class1()
        {
            BoardBitmap = new Bitmap(1500, 780); // 25列 * 60像素，13行 * 60像素
            InitializeBoard();
            PlaceBlackCells();

            // 加载背景图片和墙图片
            string roadPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "gif", "road.png");
            string wallPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "gif", "wall.png");


            RoadImage = System.Drawing.Image.FromFile(roadPath);
            WallImage = System.Drawing.Image.FromFile(wallPath);
        }

        // 初始化棋盘，将前13行25列设置为0
        private void InitializeBoard()
        {
            for (int i = 0; i < 13; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    Board[i, j] = 0;
                }
            }
        }

        // 被注释掉的方法，用于显示位图和文本
        /* 
        public virtual void Show(Bitmap bmp)
        {
            FSky.Render.Show(bmp, image, x, y);
            FSky.Render.ShowText(bmp, "bk", x - 10, y - 10);
        }
        */

        // 在棋盘上随机放置黑色方块
        private void PlaceBlackCells()
        {
            Random rand = new Random();
            int totalCells = 15 * 25;
            int blackCellsNeeded = totalCells / 2 + 1;

            while (CountBlackCells() < blackCellsNeeded)
            {
                int x = rand.Next(0, 25);
                int y = rand.Next(0, 15);
                if (Board[y, x] == 0 && !WouldForm2x2Block(x, y))
                {
                    Board[y, x] = 1;
                }
            }
        }

        // 统计黑色方块的数量
        public int CountBlackCells()
        {
            int count = 0;
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    if (Board[i, j] == 1)
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        // 检查是否形成2x2的黑色块
        public bool WouldForm2x2Block(int x, int y)
        {
            if (x >= 24 || y >= 14) return false;
            return (Board[y, x + 1] == 1 && Board[y + 1, x] == 1 && Board[y + 1, x + 1] == 1);
        }

        // 获取指定位置的单元格值
        public int GetCellValue(int x, int y)
        {
            return Board[y, x];
        }

        // 绘制棋盘
        public void Draw(Graphics g)
        {
            for (int i = 0; i < 25; i++) // x方向，25列
            {
                for (int j = 0; j < 13; j++) // y方向，13行
                {
                    // 绘制背景图片
                    if (RoadImage != null)
                    {
                        g.DrawImage(RoadImage, i * cellSize, j * cellSize, cellSize, cellSize);
                    }

                    // 如果是墙，绘制墙图片
                    if (Board[j, i] == 1 && WallImage != null)
                    {
                        g.DrawImage(WallImage, i * cellSize, j * cellSize, cellSize, cellSize);
                    }
                }
            }
        }
    }
}